package com.rest.demo.Controller;

import cn.hutool.core.util.RandomUtil;

import com.alibaba.fastjson.JSONObject;
import com.rest.demo.Service.DysmtIDCard;

import com.rest.demo.repository.DyRcUser;
import com.rest.demo.repository.IdCardJson;
import com.rest.demo.util.Md5;
import lombok.Data;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.*;

@Data
@RestController

@RequestMapping(value = "/DysmtIdCard",method = RequestMethod.POST)//整个路由路径


@CrossOrigin(origins = "https://sfz.dysmt.cn")
public class GetIdCard {



    @Value("${APiUrl.DysmtCardappKey}")
    String DysmtCardappKey;//企业异常目录请求地址
    @Value("${APiUrl.DysmtCardIdAppToken}")
    String DysmtCardIdAppToken;//企业异常目录请求令牌a

    long timeStamp=new Date().getTime();
    String time3 = new Timestamp(Calendar.getInstance().getTimeInMillis()).toString();



//vue要请求post 必须用 @ResponseBody 进行JSon的请求，并且参数必须为小写否则无法传递过来
    @ResponseBody

    @PostMapping(value ="/GetCardInfo")//通过身份证及姓名查询
    public JSONObject MDF(@RequestBody IdCardJson IdCard){//这里必须用必须用 @ResponseBody

System.out.println(IdCard.getIdcard());
        DysmtIDCard hh =new DysmtIDCard();
//        JSONObject JsonRZ = JSON.parseObject(hh.GetCard("513021198510044013"));
        return   hh.GetCard(IdCard.getIdcard(),DysmtCardIdAppToken,DysmtCardappKey);
    }


    @RequestMapping(value = "/GetRequestIdCard")//通过身份证及姓名查询
    public String GetRequestIdCard(){

//System.out.println(getSecondTimestampTwo(new Date()));
Long Time=System.currentTimeMillis()/1000;
String nonce =RandomUtil.randomNumbers(6);
String sfzh="510603198511186678";
        return  RequestIdCard(nonce,sfzh,Time);
    }

    public String RequestIdCard(String nonce ,String sfzh,Long timetamp){
//        nonce=997355&sfzh=632323190605261749&timestamp=1626251121e9pmiyrd2xnqel0mle949482e8wavipl
        String Appkey =DysmtCardappKey;
    String parameter = "nonce="+nonce+"&sfzh="+sfzh+"&timestamp="+timetamp+DysmtCardIdAppToken;
//    System.out.println(parameter);
    String Md5Parameter = Md5.MD5Small(parameter);
        return Md5Parameter;
    }




    public static int getSecondTimestampTwo(Date date){
        if (null == date) {
            return 0;
        }
        String timestamp = String.valueOf(date.getTime()/1000);
        return Integer.valueOf(timestamp);
    }


}
